package jmu.cze.utl;
import com.fasterxml.jackson.databind.ObjectMapper;
import jmu.cze.pojo.FlightStatus;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;

public class FlightStatusCrawler {

    private static final RestTemplate restTemplate;
    static {
        restTemplate = new RestTemplate();
    }

    public static FlightStatus getFlightStatus(String flightNo, String startDate){
        String jsonStr = restTemplate.getForObject(
                "https://touch.qunar.com/h5/flight/flightStatus/flightStatusList?flightNo={1}&startDate={2}&_=1592736335078",
                String.class,flightNo, startDate);
        // System.out.println(jsonStr);
        ObjectMapper mapper = new ObjectMapper();
        FlightStatus flightStatus = null;
        try {
            flightStatus = mapper.readValue(jsonStr, FlightStatus.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return flightStatus;
    }
}
